import { TODO_ADD, TODO_DEL, TODO_UPDATEBYID } from '../action/actionType';

const initState = {
  list: [
    {
      id: 1,
      task: '学习React',
      isDone: false,
    },
    {
      id: 2,
      task: '学习Vue',
      isDone: true,
    },
    {
      id: 3,
      task: '学习JS',
      isDone: false,
    },
  ],
  type: 'all',
};
export default function todoReducer(state = initState, action) {
  switch (action.type) {
    case TODO_DEL:
      return {
        ...state,
        list: state.list.filter((item) => item.id !== action.payload),
      };

    case TODO_UPDATEBYID:
      return {
        ...state,
        list: state.list.map((item) => ({
          ...item,
          isDone: item.id === action.payload ? !item.isDone : item.isDone,
        })),
      };

    case TODO_ADD:
      return {
        ...state,
        list: [{ id: Date.now(), isDone: false, task: action.payload }, ...state.list],
      };

    default:
      return state;
  }
}
